// pages/place/place.js
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		logs: '',
		http: getApp().globalData.map,
		num: [],
		state: 1,
		value: ['', '请选择门店'],
		itm: 0,
		sure: 0,
		ewyf:0,
		map:[],
		pd: '',
		add: '',
		uid: '',
		lit: '',
		txt: [],
		qid: '',
		din:[],
		qian:'',
		yunfei:0,
		ewyf:0,
		gudingzhuangtai:[]
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(res) {
		let that=this
		this.setData({
			cars:res.cars
		})
		wx.getStorage({
			key: 'uid',
			success: (e) => {
				wx.request({
					url: getApp().globalData.http + "action=gwcJS&cars=" + this.data.cars + "&userid=" + e.data.uid,
					success: (t) => {
						console.log(t)
						if(this.data.add==''){
							this.setData({
							add: t.data.data.addr,
							})
						}
						let yuanjia=t.data.data.shopCar
						if(this.data.add.Prov=='内蒙古自治区'||this.data.add.Prov=='西藏自治区'||this.data.add.Prov=='青海省'||this.data.add.Prov=='宁夏回族自治区'||this.data.add.Prov=='新疆维吾尔自治区'){
							t.data.data.shopCar.forEach(function(item,index){
								t.data.data.shopCar[index].yunfei=item.yunfei+20
							})
							this.setData({
								ewyf:20*t.data.data.shopCar.length,
								lsewyf:20
							})
						}else{
							t.data.data.shopCar=yuanjia
							this.setData({
								ewyf:0,
								lsewyf:0
							})
						}
						let num1 = this.data.num
						let k = Number('')
						let map=[]
						let m=[]
						for (let i in t.data.data.shopCar) {
							map[i]=0
							m[i]=''
							for (let d in t.data.data.shopCar[i].Cart) {
								k += Number(t.data.data.shopCar[i].Cart[d].pnum)
							}
							num1.push(k)
							k = Number('')
						}
						if (t.data.data.addr == null) {
							this.setData({
								map:map,
								din:m,
								logs: t.data.data,
								num: num1,
								pd: 0,
								uid: e.data.uid,
							})
						} else {
							this.setData({
								map:map,
								din:m,
								logs: t.data.data,
								num: num1,
								pd: 1,
								uid: e.data.uid,
							})
							
						}
						
						let c=[]
						let sta=[]
						for (let i in t.data.data.shopCar) {
							let a = []
							for (let g in t.data.data.shopCar[i].Cart) {
								a.push(this.data.logs.shopCar[i].Cart[g].ID)
							}
							a=a.join()
							sta.push('')
							c.push(a)
							console.log(sta)
							this.setData({
								list:c,
								txt:sta
							})
						}
					}
				})
			}
		})
	},
	yunxin(){
		wx.getStorage({
			key: 'uid',
			success: (e) => {
				wx.request({
					url: getApp().globalData.http + "action=gwcJS&cars=" + this.data.cars + "&userid=" + e.data.uid,
					success: (t) => {
						console.log(t)
						if(this.data.add==''){
							this.setData({
							add: t.data.data.addr,
							})
						}
						let yuanjia=t.data.data.shopCar
						if(this.data.add.Prov=='内蒙古自治区'||this.data.add.Prov=='西藏自治区'||this.data.add.Prov=='青海省'||this.data.add.Prov=='宁夏回族自治区'||this.data.add.Prov=='新疆维吾尔自治区'){
							t.data.data.shopCar.forEach(function(item,index){
								t.data.data.shopCar[index].yunfei=item.yunfei+20
							})
							this.setData({
								ewyf:20*t.data.data.shopCar.length,
								lsewyf:20
							})
						}else{
							t.data.data.shopCar=yuanjia
							this.setData({
								ewyf:0,
								lsewyf:0
							})
						}
						let num1 = this.data.num
						let k = Number('')
						let map=[]
						let m=[]
						for (let i in t.data.data.shopCar) {
							map[i]=0
							m[i]=''
							for (let d in t.data.data.shopCar[i].Cart) {
								k += Number(t.data.data.shopCar[i].Cart[d].pnum)
							}
							num1.push(k)
							k = Number('')
						}
						if (t.data.data.addr == null) {
							this.setData({
								map:map,
								din:m,
								logs: t.data.data,
								num: num1,
								pd: 0,
								uid: e.data.uid,
							})
						} else {
							this.setData({
								map:map,
								din:m,
								logs: t.data.data,
								num: num1,
								pd: 1,
								uid: e.data.uid,
							})
							
						}
						
						let c=[]
						for (let i in t.data.data.shopCar) {
							let a = []
							for (let g in t.data.data.shopCar[i].Cart) {
								a.push(this.data.logs.shopCar[i].Cart[g].ID)
							}
							a=a.join()
							c.push(a)
							this.setData({
								list:c,
							})
						}
					}
				})
			}
		})
	},
	none(e) {
		this.setData({
			state: 0,
			xb: e.currentTarget.dataset.id,
		})
	},
	none1(e) {
		this.setData({
			state: 1,
			xb: e.currentTarget.dataset.id,
		})
	},
	index(e) {
		let c=0
		let a=this.data.map
		let that=this
		a[this.data.xb]=e.detail.value
		a.forEach(function(item,index){
			if(item=="1"){
				c+=that.data.logs.shopCar[that.data.xb].yunfei
			}
		})
		this.setData({
			itm: a,
			weiz:e.detail.value,
			yunfei:c
		})
	},
	xz() {
		let a=this.data.map[this.data.xb]
		a=this.data.itm
		if (this.data.value[1] != '请选择门店') {
			this.setData({
				state: 1,
				map: a,
			})
		} else {
			if (this.data.weiz != 0) {
				wx.showToast({
					icon: "none",
					title: '请选择自售门店'
				})
			} else {
				this.setData({
					state: 1
				})
			}
		}
	},
	slit(e) {
		this.setData({
			lit: e.detail.value
		})
	},
	onShow(){
		let a=this.data.din
		if(this.data.value!=undefined){
			a[this.data.xb]=this.data.value[0]
			this.setData({
				din:a
			})
		}
		this.yunxin()
	},
	deal() { 
		console.log(this.data.add)
		if(this.data.add!=null&&this.data.add.ADDR_ID!=''&&this.data.add!=''){
			let a = []
				for (let i in this.data.logs.shopCar) {
					for (let g in this.data.logs.shopCar[i].Cart) {
						console.log(this.data.din[i])
						a.push(JSON.stringify({
							"shopID": this.data.logs.shopCar[i].shopID,
							"cartList": this.data.logs.shopCar[i].Cart[g].ID,
							"pstype": this.data.map[i],
							"mdid": this.data.din[i],
							"qid": this.data.qid,
							"remark": this.data.lit
						}))
					}
				let asd = a.join()
				console.log(a)
				wx.request({
					url: getApp().globalData.http2 + 'action=gwcUpOrder&userid=' + this.data.uid + '&addrid=' + this.data.add.ADDR_ID +
						'&cars=[' + a + ']',
					success: (e) => {
						console.log(e)
						if(e.data.data.orderCode==undefined){
							wx.showToast({
								title:'数据错误，请返回首页重新购买',
								icon:'none'
							})
							}else{
								wx.navigateTo({
									url: '../Vip/deal/deal?orderCode=' + e.data.data.orderCode + '&id=' + e.data.data.ID + "&totalprice=" + e.data
										.data.totalprice
								})
							}
					}
				})
			}
			console.log(this.data.sure)
		}else{
			wx.showToast({
				icon:'none',
				title:'请填写地址'
			})
		}
	},
	moon(e) {
		console.log(e)
		let cac=this.data.txt.join()
		if (this.data.logs.shopCar[e.currentTarget.dataset.id].quanList.length > 0) {
			var listData = JSON.stringify(this.data.logs.shopCar[e.currentTarget.dataset.id].quanList)
			wx.navigateTo({
				url: '../place/quan/quan?data=' + listData+'&chang='+cac+'&xb='+e.currentTarget.dataset.id
			})
		} else {
			wx.showToast({
				icon: 'none',
				title: '暂无可用优惠券'
			})
		}
	}
})
